package com.eulog.gwt.euloggwtlibrary.user.client.ui;

import java.util.ArrayList;

import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.LoadListener;
import com.google.gwt.user.client.ui.Widget;

public class EulogImage extends Image {

	private static int eulogImageCount=0;
	private static Timer timer;
	private static ArrayList<EulogImage> uncompletedImages = new ArrayList<EulogImage>(); // After GWT 1.5.0
	private static boolean timerRunning = false;
	
	static {
		if(EulogDOM.isIE6())
		{
			timer = new Timer() {
				
				@Override
				public void run() {
					//RootPanel.get().add(new Label("Timer Run : " + uncompletedImages.size()));
					if(uncompletedImages.size()>0) {
						for (EulogImage ei : uncompletedImages) {
							ei.setFireOnloadIfNotFiredCorrectlyEventIE6();
						}
					}
					else {
						cancel();
						timerRunning=false;
						//RootPanel.get().add(new Label("Timer Canceled"));
					}
				}
			};
		}
	}
	
	protected boolean complete;
	protected boolean load;
	protected boolean error;
	
	/*
	public class EulogImageRootPanelLoadListener implements LoadListener {
		public void onError(Widget sender) {
		}
		
		public void onLoad(Widget sender) {
			RootPanel.get().add(new Label("ROOTPANEL LOADED"));
		}
	}
	*/
	
	public EulogImage() {
		super();
		init(false);
	}
	
	public EulogImage(java.lang.String url) {
		super(url);
		init(false);
	}
	
	public EulogImage(java.lang.String url,boolean setOriginalSizeAfterLoading) {
		super(url);
		init(setOriginalSizeAfterLoading);
	}
	
	/*
	protected EulogImage(Element e) {
		super(e);
		init(false);
	}
	*/
	
	private boolean isAlreadyLoadedIE6() {
		if(EulogDOM.isIE6() && EulogDOM.isCompleted(this.getElement()) && complete==false) return true;
		return false;
	}
	
	private void init(boolean setOriginalSizeAfterLoading) {
		eulogImageCount++;
		uncompletedImages.add(this);
		
		if(EulogDOM.isIE6()) {
			if(timerRunning==false) {
				timer.scheduleRepeating(2000);
				timerRunning=true;
			}
		}
		
		
		complete=false;
		load=false;
		error=false;
		
		addLoadListener(new EulogImageLoadListener(setOriginalSizeAfterLoading));
	
		setFireOnloadIfNotFiredCorrectlyEventIE6();
	}
	
	private void setFireOnloadIfNotFiredCorrectlyEventIE6() {
		if(isAlreadyLoadedIE6()) {
			onBrowserEvent(EulogDOM.getIE6OnloadEvent());
			//RootPanel.get().add(new Label(getUrl() + " custom onload fired"));
		}
	}
	
	public class EulogImageLoadListener implements LoadListener {
		private boolean eulogImageLoadListenerSetOriginalSizeAfterLoading;
		public EulogImageLoadListener(boolean setOriginalSizeAfterLoading) {
			super();
			eulogImageLoadListenerSetOriginalSizeAfterLoading  = setOriginalSizeAfterLoading;
		}
		public void onLoad(Widget sender) {
			uncompletedImages.remove(sender);
			if(load==false) {
				complete=true;
				load=true;
				
				if(eulogImageLoadListenerSetOriginalSizeAfterLoading) {
					setVisibleRect(0, 0, getWidth(),getHeight());
					//RootPanel.get().add(new Label(getUrl() + " load, x:" + getWidth() + "  y:" + getHeight()));
				}
				else {
					//RootPanel.get().add(new Label(getUrl() + " load"));
				}
			}
		}
		
		public void onError(Widget sender) {
			complete=true;
			error=true;
			uncompletedImages.remove(sender);
			
			setVisible(false);
			removeFromParent();
			//RootPanel.get().add(new Label(getUrl() + " error"));
		}
	}
	
	@Override
	public void removeFromParent() {
		super.removeFromParent();
		
		if(EulogDOM.isIE6()) {
			uncompletedImages.remove(this);
		}
	}
	
	public boolean isCompleted() {return complete;}
	public boolean isLoaded() {return load;}
	public boolean isError() {return error;}
}
